﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MixScriptWeb.control;
using MixScriptWeb.Share;
using MixScriptWeb.AppCode.gjips;


namespace MixScriptWeb.Jideco.gjips
{
    public enum ctrlType
    {
        tulisan,
        tanggal

    }

    public class cControl
    {
        public Label lblCaption;
        public Label lblttk;
        public MixScriptTB mx;
        public TextBox tbtgl;
        public ctrlType cttype;
        public string oVal
        {
            get
            {
                if (cttype == ctrlType.tanggal)
                {
                    return tbtgl.Text;
                }
                else
                {
                    return mx.Text;
                }
            }

        }


        public cControl()
        {

        }
    }
    public partial class fMatGjips : cCommonWeb, IMIxCommon
    {
        private static DataTable dt;
        private static SortDirection sd;
        private static cRefGjips oReff;
        private static string stblname = "";
        private static string reftype;



        private struct innitparam
        {
            public bool isvisible;
            public ctrlType ctType;
            public bool isfind;
            public string searchid;
            public string tCaption;
            public bool islike;

        }





        private static innitparam[] ip;

      private static  List<cControl> lc = new List<cControl>();


        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                string[] sheadertext = new string[] { "" };
                string[] sdatavalue = new string[] { "" };



                if (!Page.IsPostBack)
                {
                    lc.Clear();
                    
                    collectControl();
                     reftype = Request.QueryString["reftype"].ToString();
                    //string reftype="2";

                    if (reftype == "1")//
                    {
                        stblname = "vw_joid_group_material_v";
                        lblTitle.Text = "Group Material";
                        sheadertext = new string[] { "Part No.", "Main Supply Place Type", "Main Supply Place Code", "Warehouse Type", "Warehouse Code" };
                        sdatavalue = new string[] { "itno", "PDLC_Business_Code", "Primary_Deliver_LOC_Code", "SC_Business_Code", "SubInventory_Code" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Part No";
                        ip[0].isfind = false;
                        ip[0].islike = true;

                        //ip[0].ctType = ctrlType.tulisan;
                        //ip[0].isvisible = true;
                        //ip[0].tCaption = "Measurement Code";
                        //ip[0].isfind = false;
                        ////ip[0].islike = true;

                        


                    }
                    else if (reftype == "2")//
                    {
                        stblname = "vw_joid_unit_material_v";
                        lblTitle.Text = "Unit Material";
                        sheadertext = new string[] { "Measurement Code", "Measurement Name", "Registration Date" };
                        sdatavalue = new string[] { "unms", "unmsns", "rgdt" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Measurement Code";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Measurement Name";
                        ip[1].isfind = false;
                        ip[1].islike = true;

                    }
                    else if (reftype == "3")
                    {
                        stblname = "vw_joid_customer_v";
                        lblTitle.Text = "Customer";
                        sheadertext = new string[] { "Customer Type", "Customer Code", "Customer Name", "Customer Short Name", "Address1", "Address2", "Telephone  No.", "Fax No.", "Mail Address", "Web URL", "Country", "Currency" };
                        sdatavalue = new string[] { "codfbpidbc", "codfbpid", "bpidnm", "bpidns", "ad1", "ad2", "tlno", "fxno", "mailno", "wbno", "cntyns", "momsns" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Customer Code";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Customer Name";
                        ip[1].isfind = false;
                        ip[1].islike = true;

                    }
                    else if (reftype == "4")
                    {
                        stblname = "vw_joid_supplier_v";
                        lblTitle.Text = "Supplier";
                        sheadertext = new string[] { "Supplier Type", "Supplier Code", "Supplier Name", "Supplier Short Name", "Address1", "Address2", "Post No.", "Telephone  No.", "Fax No.", "Mail Address", "Web URL", "Country", "Currency" };
                        sdatavalue = new string[] { "codfbpidbc", "codfbpid", "bpidnm", "bpidns", "ad1", "ad2", "postno", "tlno", "fxno", "mailno", "wbno", "cntyns", "momsns" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Supplier Code";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Supplier Name";
                        ip[1].isfind = false;
                        ip[1].islike = true;

                    }
                    else if (reftype == "5")
                    {
                        stblname = "vw_joid_all_parts_v";
                        lblTitle.Text = "All Parts";
                        sheadertext = new string[] { "Part No.", "Part Name", "Drawing No.", "Part Name Type", "Design Notice No.", "Contract Unit Code", "Production Unit Code", "QTY. of Contract Unit", "QTY. of Production Unit", "Unit of Weight", "Weight of Parts" };
                        sdatavalue = new string[] { "itno", "itnm", "dwno", "itnmcl", "itnocgno", "tdms", "usms", "tdmsqt", "usmsqt", "sdqtwt", "wtms" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Part No";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Part Name";
                        ip[1].isfind = false;
                        ip[1].islike = true;

                    }
                    else if (reftype == "6")
                    {
                        stblname = "vw_joid_bill_material_v";
                        lblTitle.Text = "Bill Of Material";
                        sheadertext = new string[] { "Parents Part No.", "RevisionNo.", "Module Type", "Module Code", "Sequential No.", "Composed Part No.", "Module Type", "Module Code", "QTY. of Composed", "Design Notice No.", "Application Date", "Lapse Date" };
                        sdatavalue = new string[] { "itno", "bmrvno", "paitmfbpbc", "paitmfbp", "itsrli", "chitno", "chitpcbc", "chitpc", "usqtnmr", "sritcgno", "apdt", "aptodt" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Parents Part No.";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Composed Part No.";
                        ip[1].isfind = false;
                        ip[1].islike = true;

                    }
                    else if (reftype == "7")
                    {
                        stblname = "vw_joid_sales_unit_price_v";
                        lblTitle.Text = "Sales Unit Price";
                        sheadertext = new string[] { "Customer Part No.", "Customer Code", "Export Flag", "Application Date", "Lapse Date", "Currency", "Unit Price" };
                        sdatavalue = new string[] { "cuitno", "cuid", "epfg", "apdt", "aptodt", "momsns", "tdup" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Customer Part No.";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Customer Code";
                        ip[1].isfind = false;
                        ip[1].islike = true;
                    }
                    else if (reftype == "8")
                    {
                        stblname = "vw_joid_purchase_unit_price_v";
                        lblTitle.Text = "Purchase Unit Price";
                        sheadertext = new string[] { "Part No.", "Part No.", "Supplier Code", "Application Date", "Lapse Date", "Currency", "Unit Price" };
                        sdatavalue = new string[] { "itno", "pcidbc", "pcid", "apdt", "aptodt", "momsns", "orup" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Part No.";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Supplier Code";
                        ip[1].isfind = false;
                        ip[1].islike = true;
                    }

                    else if (reftype == "9")
                    {
                        stblname = "vw_joid_module_v";
                        lblTitle.Text = "Modul";
                        sheadertext = new string[] { "Module Type", "Module Code", "Module Name", "Module Short Name" };
                        sdatavalue = new string[] { "codfbpidbc", "codfbpid", "bpidnm", "bpidns" };
                        ip = new innitparam[5];
                        ip[0].ctType = ctrlType.tulisan;
                        ip[0].isvisible = true;
                        ip[0].tCaption = "Module Code";
                        ip[0].isfind = false;
                        //ip[0].islike = true;

                        ip[1].ctType = ctrlType.tulisan;
                        ip[1].isvisible = true;
                        ip[1].tCaption = "Module Name";
                        ip[1].isfind = false;
                        ip[1].islike = true;
                    }

                    //addFindClickAttribute(this, new MixScriptTB[] { txtPartno }, new string[] { "SRCMATGJ" });
                    //txtItNo.ReadOnly = true;
                    //  setSortExpression(gvdetail, new string[] { "banknum", "bankname", "address", "city", "country", "ac" });

                    //  txtPartno.Tbx.Attributes.Add("readonly", "readonly");
                    setControl();
                    initGvColumn(gvdetail, sheadertext, sdatavalue);
                }
                mn = (MainMenu)this.Master;
                hideStatusBar();
                // oReff = new cReferences();
                // if(gvdetail.Rows.Count>0)


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }


        }
        private void collectControl()
        {

            cControl oc = null;
            oc = new cControl();
            oc.cttype = ctrlType.tulisan;
            oc.lblCaption = Label1;
            oc.lblttk = Label6;
            oc.mx = txt1;
            oc.tbtgl = tgl1;
            lc.Add(oc);
            oc = new cControl();
            oc.cttype = ctrlType.tulisan;
            oc.lblCaption = Label2;
            oc.lblttk = Label7;
            oc.mx = txt2;
            oc.tbtgl = tgl2;
            lc.Add(oc);
            oc = new cControl();
            oc.cttype = ctrlType.tulisan;
            oc.lblCaption = Label3;
            oc.lblttk = Label8;
            oc.mx = txt3;
            oc.tbtgl = tgl3;
            lc.Add(oc);
            oc = new cControl();
            oc.cttype = ctrlType.tulisan;
            oc.lblCaption = Label4;
            oc.lblttk = Label9;
            oc.mx = txt4;
            oc.tbtgl = tgl4;
            lc.Add(oc);
            oc = new cControl();
            oc.cttype = ctrlType.tulisan;
            oc.lblCaption = Label5;
            oc.lblttk = Label10;
            oc.mx = txt5;
            oc.tbtgl = tgl5;
            lc.Add(oc);
        }

        private void setControl()
        {
            try
            {
                int k = 0;
                foreach (innitparam i in ip)
                {
                    if (i.isvisible)
                    {
                        if (i.ctType == ctrlType.tulisan)
                        {
                            lc[k].tbtgl.Visible = false;
                            if(i.islike)
                            lc[k].mx.InquiryMethod = InqMethod.Like;
                        }
                        else
                        {
                            lc[k].mx.Visible = false;
                        }
                        if(i.searchid!=null)
                        if (i.searchid.Trim() != string.Empty)
                        {
                            addFindClickAttribute(this,new MixScriptTB[] { lc[k].mx}, new string[] { i.searchid });
                        }

                        if (i.tCaption.Trim() != string.Empty)
                        {
                            lc[k].lblCaption.Text = i.tCaption;
                        }


                    }
                    else
                    {
                        lc[k].lblCaption.Visible = false;
                        lc[k].lblttk.Visible = false;
                        lc[k].mx.Visible = false;
                        lc[k].tbtgl.Visible = false;
                    }
                    k+=1;
                    

                }
            }
            catch (Exception ex)
            {

                throw ex;
            }
        }

        protected void gvdetail_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {

                //oUM = new cUserManagement();

                if (gvdetail.SelectedIndex > -1)
                {
                    //txtPartno.Text = gvdetail.SelectedRow.Cells[0].Text;
                    //txtwrhcode.Text = gvdetail.SelectedRow.Cells[2].Text;
                    //txtWrhName.Text = gvdetail.SelectedRow.Cells[3].Text;
                    //txtWrhDesc.Text = gvdetail.SelectedRow.Cells[4].Text;




                    //ini ngehapus &nbsp;
                   // removeNbsp(new MixScriptTB[] { txtPartno });


                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void gvdetail_Sorting(object sender, GridViewSortEventArgs e)
        {
            if (sd == SortDirection.Ascending)
            {
                sd = SortDirection.Descending;
            }
            else
            {
                sd = SortDirection.Ascending;
            }

            DataView dataView = new DataView(dt);
            dataView.Sort = e.SortExpression + " " + ShortDirection(sd);

            bindGridview(gvdetail, dataView);
        }

        protected void gvdetail_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            gvdetail.PageIndex = e.NewPageIndex;
            //gvdetail.DataBind();
            bindGridview(gvdetail, dt);


        }

        private Control[] fndControl()
        {
            int k = 0;
            List<Control> lcc= new List<Control>();
            foreach (innitparam i in ip)
            {
                if(i.isvisible)
                {
                    if (i.ctType == ctrlType.tulisan)
                    {

                        TextBox tb = (TextBox)this.FindControl("ctl00$ContentPlaceHolder1$txt" + (k+1).ToString() + "$txt");

                        lcc.Add(tb);

                        
                    }
                    else
                    {
                        TextBox tb = (TextBox)this.FindControl(lc[k].tbtgl.ClientID);
                        lcc.Add(tb);
                    }

                }
                k++;
            }

            return lcc.ToArray();
        }
        protected void btnFind_Click(object sender, EventArgs e)
        {
            try
            {
                oReff = new cRefGjips(); //new cUserManagement();
                dt = new DataTable();
                Control[] ctl = null; ;//=fndControl();
                if(reftype=="1" ||reftype=="2"||reftype=="5"||reftype=="7")
                {
                    ctl =fndControl();
                }
                else if(reftype=="3" || reftype=="4" || reftype=="9")
                {
                    ctl= new Control[]{null,txt1,txt2};
                }
                else if (reftype == "6")
                {
                    ctl = new Control[] { txt1, null,null,null,null,txt2 };
                }
                else if (reftype == "8")
                {
                    ctl = new Control[] {txt1 ,null, txt2};
                }               
                else
                {
                    ctl = fndControl();
                }



               if (oReff.getInquiry(ref dt, genInqParamLike(ctl), stblname, out errMsg))
               {

               }
               else
               {
                   ShowStatusBar("Record Not Found", msgType.msgError);
               }
               bindGridview(gvdetail, dt);



            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message, msgType.msgError);
            }

        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                //oReff = new cReferences();
                //string ss = "";
                //if (isvalid())
                //{
                //    if (txtwrhid.Text.Trim() == string.Empty)
                //    {

                //        if (oReff.AddNewRowDB(out errMsg, "tb_wrhmst", new int[] { 0 }, new string[] { "WRHID" }, false, txtwrhid.Text, txtwrhcode.Text, txtWrhName.Text, txtWrhDesc.Text))
                //        {
                //            ShowStatusBar(errMsg, msgType.msgSuccess);
                //            txtwrhid.Text = ss;
                //            btnFind_Click(btnFind, new EventArgs());
                //            btnClear_Click(btnClear, new EventArgs());
                //        }
                //        else
                //        {
                //            ShowStatusBar(errMsg, msgType.msgError);
                //        }

                //    }
                //    else
                //    {
                //        if (oReff.isRecordExist("tb_wrhmst", new string[] { "wrhid" }, new object[] { txtwrhid.Text}))
                //        {
                //            if (oReff.EditRowDtDB(out errMsg, "tb_wrhmst", new string[] { "wrhid" }, new object[] { txtwrhid.Text }, false, txtwrhid.Text, txtwrhcode.Text, txtWrhName.Text, txtWrhDesc.Text))
                //            {
                //                ShowStatusBar(errMsg, msgType.msgSuccess);
                //                btnFind_Click(btnFind, new EventArgs());
                //                btnClear_Click(btnClear, new EventArgs());
                //            }
                //            else
                //            {
                //                ShowStatusBar(errMsg, msgType.msgError);
                //            }

                //        }
                //        else
                //        {
                //            ShowStatusBar("Record Not Found");
                //        }
                //    }
                //}
                //else
                //{
                //    // ShowStatusBar(errMsg);
                //}
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }



        protected void btnDelete_Click(object sender, EventArgs e)
        {
            try
            {
                ////cek apakah user sudah pilih data yg akan dihapus
                //if (txtwrhid.Text != string.Empty)
                //{
                //    //cek apakah data itu ada
                //    if (oReff.isRecordExist("tb_wrhmst", new string[] { "wrhid" }, new object[] { txtwrhid.Text }))
                //    {
                //        oReff = new cReferences();

                //        //coba hapus
                //        //                                nama primary key                nilainya          nama tablenya   pesan error
                //        if (oReff.deleteRowDB(new string[] { "wrhid" }, new object[] { txtwrhid.Text }, "tb_wrhmst", out errMsg))
                //        {
                //            //jika berhasil
                //            ShowStatusBar(em.getError(8), msgType.msgSuccess);
                //        }
                //        else
                //        {   //jika gagal
                //            ShowStatusBar(errMsg);
                //        }

                //    }
                //    else
                //    {   //jika data tidak ditemukan
                //        ShowStatusBar(em.getError(7));
                //    }
                //}
                //else
                //{
                //    ShowStatusBar(em.getError(7));
                //}
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }

        protected void btnClear_Click(object sender, EventArgs e)
        {
            try
            {
                errMsg = "";
              //  setValid(new MixScriptTB[] { txtPartno, txtPartName });
              //  clearMixTB(new MixScriptTB[] { txtPartno, txtPartName });



            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }

        }



        #region IMIxCommon Members

        //validasi inputan
        public bool isvalid()
        {
            try
            {
                //errMsg = "";
                ////set semua control valid
                //setValid(new MixScriptTB[] { txtwrhid, txtwrhcode, txtWrhName, txtWrhDesc });
                ////cek semua field mandatory
                //if (MustFilledOk(new Control[] {  txtwrhcode, txtWrhName }, out errMsg))
                //{
                //    if (txtwrhcode.Text.Trim().Length == 3)
                //    {

                //    }
                //    else
                //    {
                //        ShowStatusBar(errMsg + " " + em.getError(15));
                //        return false;

                //    }

                //}
                //else
                //{
                //    ShowStatusBar(errMsg + " " + em.getError(2));
                //    return false;
                //}
                return true;
            }
            catch (Exception ex)
            {

                throw ex;
            }

        }

        #endregion




        #region Button





        #endregion


        #region Gridview
        #region Gridview Event





        protected void gvdetail_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            try
            {
                if (e.Row.DataItemIndex == -1)
                    return;
                e.Row.Attributes.Add("onMouseOver", "this.style.cursor='pointer';");
                e.Row.Attributes.Add("onclick", this.GetPostBackClientEvent(gvdetail, "Select$" + e.Row.RowIndex.ToString()));
                //e.Row.Attributes.Add("onclick", this.GetPostBackEventReference(gvdetail, "Select$" + e.Row.RowIndex.ToString())); //  (gvdetail, "Select$" + e.Row.RowIndex.ToString()));


            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }



        #endregion





        protected void btnReport_Click(object sender, EventArgs e)
        {
            try
            {

                if (dt.Rows != null)
                {
                    Session["datareport"] = dt;
                    Response.Redirect("~/tesreport.html");
                }
            }
            catch (Exception ex)
            {

                ShowStatusBar(ex.Message);
            }
        }
        //protected void cbAll_CheckedChanged(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        bool b = ((CheckBox)sender).Checked;


        //        foreach (GridViewRow gr in gvdetail.Rows)
        //        {
        //            CheckBox cb = (CheckBox)gr.Cells[0].FindControl("cbitem");
        //            cb.Checked = b;
        //        }

        //    }
        //    catch (Exception ex)
        //    {

        //        ShowStatusBar(ex.Message);
        //    }
        //}

        //protected void lbDelete_Click(object sender, EventArgs e)
        //{
        //    try
        //    {
        //        oReff = new cReferences();
        //        bool isok = true;
        //        int i = 0;
        //        foreach (GridViewRow gr in gvdetail.Rows)
        //        {

        //            if (((CheckBox)gr.Cells[0].FindControl("cbitem")).Checked)
        //            {
        //                string sid = gr.Cells[1].Text;

        //                if (!oReff.deleteRowDB(new string[] { "wrhid" }, new string[] { sid }, "tb_wrhmst", out errMsg))
        //                {
        //                    ShowStatusBar(em.getError(9) + sid + " " + errMsg);
        //                    isok = false;
        //                    continue;
        //                }
        //                else
        //                {
        //                    i++;
        //                }
        //            }

        //        }

        //        if (i == 0 && isok)
        //        {
        //            ShowStatusBar("Please Select Item First");
        //        }
        //        else if (isok)
        //        {
        //            btnClear_Click(this, new EventArgs());
        //            btnFind_Click(this, new EventArgs());
        //            ShowStatusBar("Delete Completed", msgType.msgSuccess);
        //        }
        //    }
        //    catch (Exception ex)
        //    {

        //        ShowStatusBar(ex.Message);
        //    }
        //}

        #endregion
    }
}